package de.tabuma.wsdl2xform.xform.controller;

import de.tabuma.soap.logik.*;
import de.tabuma.wsdl2xform.xform.constants;
import de.tabuma.wsdl2xform.xform.logik.*;
import de.tabuma.lib.tabumacontroller;
import java.io.*;
import java.util.*;
import java.util.logging.*;
import javax.servlet.*;
import javax.servlet.http.*;
import de.tabuma.soap.*;
import de.tabuma.soap.entity.wsdl.*;

/**
 *
 * @author $Author: alefen $
 * @version $Revision: 1.1 $
 * @since $Date: 2009/12/30 01:10:56 $
 */
public class projectPage1 extends tabumacontroller {

    private static Logger log = Logger.getLogger(projectPage1.class.getName());

    public projectPage1(String servlet, Hashtable parameter) {
        super(servlet, parameter);
    }

    public void navigate(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
        try {
            //TODO checking whether url is valide
            if(request.getParameter(constants.NAV_NEXT_STEP)!=null) {

                String name = request.getParameter(constants.PROJECT_NAME);
                String description = request.getParameter(constants.PROJECT_DESCRIPTION);
                String wsdlUrl = request.getParameter(constants.WSDL_URL);

                log.fine("wsdlUrl:"+wsdlUrl);

                TDefinitions wsdlFile = WsdlFactory.loadWsdlUrl(wsdlUrl);

                log.fine("count:"+wsdlFile.getAnyTopLevelOptionalElement().size());

                log.fine("start storing wsdlFile in DB.");

                long key = wsdlFile.getId();

                log.fine("loading wsdlfile is ready");

                log.fine("loading message list is ready");

                List<String> messageList1 = new ArrayList<String>();
                List<Long> operationList1 = SoapFacade.getAllOperation(wsdlFile);
                for(int i=0;i<operationList1.size();i++) {
                    TOperation operation = PMF.get(operationList1.get(i), new TOperation());
                    messageList1.add(operation.getName());
                }
                log.fine("setting message list to string list");

                request.setAttribute(constants.METHOD_LIST,messageList1);
                request.setAttribute(constants.XFORM_NAV,""+constants.NAV_SECOND_STEP);
                request.setAttribute(constants.T_DEFINITIONS,wsdlFile);
                request.setAttribute(constants.PROJECT_NAME,name);
                request.setAttribute(constants.PROJECT_DESCRIPTION, description);
                request.setAttribute(constants.WSDL_URL, wsdlUrl);
                request.setAttribute(constants.T_DEFINITIONS_KEY, ""+key);
            }
            else {
                request.setAttribute(constants.XFORM_NAV,""+constants.NAV_FIRST_STEP);
            }
        }
        catch(Exception e) {
            log.warning(e.getMessage());
        }
    }

}
